package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.dto.SetmealDto;
import com.example.entity.Category;
import com.example.entity.Dish;
import com.example.entity.Setmeal;
import com.example.entity.SetmealDish;
import com.example.service.CategoryService;
import com.example.service.SetmealDishService;
import com.example.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    //新增套餐
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("获取的值:{}",setmealDto);

        setmealService.saveWithDish(setmealDto);

        return R.success("添加成功");
    }
    //分页查询
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //分页构造器
        Page<Setmeal> pageInfo=new Page<>(page,pageSize);
        Page<SetmealDto> pageDto=new Page<>();
        //查询条件构造器
        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
        lqw.eq(name!=null,Setmeal::getName,name);
        lqw.orderByAsc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo,lqw);
        //对象拷贝
        BeanUtils.copyProperties(pageInfo,pageDto,"records");

        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> list=records.stream().map((item)->{
            SetmealDto setmealDto=new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(item,setmealDto);
            //分类id
            Long categoryId = item.getCategoryId();
            //根据分类id查询分类对象
            Category category = categoryService.getById(categoryId);
            if (category!=null){
                //分类名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());

        pageDto.setRecords(list);

        return R.success(pageDto);
    }
    //删除套餐
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);

        setmealService.removeWithDish(ids);

        return R.success("删除成功");
    }
    //根据id查询数据实现回显
    @GetMapping("/{id}")
    public R<SetmealDto> getupdate(@PathVariable Long id){
        log.info("id:{}",id);

        SetmealDto setmealDto = setmealService.getByIdWithDish(id);

        return R.success(setmealDto);
    }

    //修改套餐
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        log.info("setmealDto:{}",setmealDto);

        setmealService.updateWithDish(setmealDto);

        return R.success("修改成功");
    }
    //启停售状态
    @PostMapping("/status/{status}")
    public R<String> statusupdate(@PathVariable int status,Long[] ids){
        log.info("status:{}",status);
        log.info("ids:{}",ids);
        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId,ids);
        Setmeal setmeal=new Setmeal();
        setmeal.setStatus(status);
        setmealService.update(setmeal,lqw);
        return R.success("状态修改成功");
    }
    //根据条件查询套餐信息
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        log.info("setmeal:{}",setmeal);
        //条件构造器
        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
        lqw.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        lqw.eq(Setmeal::getStatus,1);
        lqw.orderByAsc(Setmeal::getCreateTime).orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(lqw);

        return R.success(list);
    }

//    @GetMapping("/list")
//    public R<List<SetmealDto>> list(Setmeal setmeal){
//        log.info("setmeal:{}",setmeal);
//        //条件构造器
//        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
//        lqw.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
//        lqw.eq(Setmeal::getStatus,1);
//        lqw.orderByAsc(Setmeal::getCreateTime).orderByDesc(Setmeal::getUpdateTime);
//
//        List<Setmeal> list = setmealService.list(lqw);
//
//        List<SetmealDto> setmealDtoList = list.stream().map((item) -> {
//            SetmealDto setmealDto = new SetmealDto();
//
//            BeanUtils.copyProperties(item, setmealDto);
//
//            Long categoryId = item.getCategoryId();//分类id
//            //根据id查询分类对象
//            Category category = categoryService.getById(categoryId);
//            if (category != null) {
//                String categoryName = category.getName();
//                setmealDto.setCategoryName(categoryName);
//            }
//            Long setmealId = item.getId();
//            LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.eq(SetmealDish::getSetmealId, setmealId);
//            List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);
//            setmealDto.setSetmealDishes(setmealDishes);
//            return setmealDto;
//        }).collect(Collectors.toList());
//
//        return R.success(setmealDtoList);
//
//    }
}
